package org.qiyu.live.api.controller;

import org.apache.dubbo.config.annotation.DubboReference;
import org.qiyu.live.user.dto.UserDTO;
import org.qiyu.live.user.interfaces.IUserRpc;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author 朝问道
 * @Date 2023年12月28日 10:27
 * @Description
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @DubboReference //引入dubbo服务
    private IUserRpc iUserRpc;


    @RequestMapping("getUserInfo")
    public UserDTO getUserInfo(Long userId) {
        return iUserRpc.getByUserId(userId);
    }


    @RequestMapping("insertOneUser")
    public boolean insertOneUser() {
        //构造用户数据
        UserDTO userDTO = new UserDTO();
        long currentTimeMillis = System.currentTimeMillis() % 10000;
        userDTO.setUserId(currentTimeMillis);
        userDTO.setSex(0);
        userDTO.setNickName("小明" + currentTimeMillis);

        return iUserRpc.insertUserInfo(userDTO);
    }

    @RequestMapping("updateUserInfo")
    public boolean updateUserInfo(Long userId) {


        UserDTO userDTO = new UserDTO();
        userDTO.setUserId(userId);
        userDTO.setNickName("修改了" + System.currentTimeMillis());

        return iUserRpc.updateUserInfo(userDTO);
    }

    @GetMapping("/batchQueryUserInfo")
    public Map<Long, UserDTO> getUserInfo(String userIdStr) {
        List<Long> userIds = Arrays.asList(userIdStr.split(",")).stream().map(id -> Long.valueOf(id)).collect(Collectors.toList());
        return iUserRpc.batchQueryUserInfo(userIds);
    }

}
